package com.higgins.eventtracking.client.finance;

import java.util.Date;
import java.util.List;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.VisualizationUtils;
import com.google.gwt.visualization.client.AbstractDataTable.ColumnType;
import com.google.gwt.visualization.client.visualizations.LineChart;
import com.google.gwt.visualization.client.visualizations.LineChart.Options;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.IncomeSettingsService;
import com.higgins.eventtracking.client.IncomeSettingsServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.ChartDataDTO;
import com.higgins.eventtracking.client.dto.TransactionDTO;

public class Income extends VerticalPanel		{
	
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";
	
	private final IncomeSettingsServiceAsync incomeSettingsService = GWT
			.create(IncomeSettingsService.class);
	
	
	private final Button sendButton = new Button("Add");
	private final TextBox incomeTextBox = new TextBox();
	private final TextBox outgoingTextBox = new TextBox();
	private final TextBox severableOutgoingTextBox = new TextBox();
	private final Label errorLabel = new Label();
	private final DialogBox dialogBox = new DialogBox();
	private final Button closeButton = new Button("Close");
	private final Label textToServerLabel = new Label();
	private final HTML serverResponseLabel = new HTML();
	private boolean firstRun = true;
	
	public Income()			{
	}
	
	public void update()	{
		if (firstRun)	{
			initialise();
		} else {
			refresh();
		}
	}
	public void initialise()	{
		firstRun = false;
		setUpMainPage();
		setUpDialogBox();
		setUpHandlers();
		requestIncomeAndOutgoing();		
	}
	public void refresh()	{
		
	}
	private void setUpMainPage()	{
		incomeTextBox.setText("0.0");
		outgoingTextBox.setText("0.0");
		severableOutgoingTextBox.setText("0.0");
		sendButton.addStyleName("sendButton");

		add(new Label("Income:"));
		add(incomeTextBox);
		add(new Label("Outgoings:"));
		add(outgoingTextBox);
		add(new Label("Severable Outgoing:"));
		add(severableOutgoingTextBox);
		add(sendButton);
	}
	
	private void setUpDialogBox()	{
		// Create the popup dialog box
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});
	}
	
	private void setUpHandlers()	{
		class MyHandler implements ClickHandler, KeyUpHandler {
			public void onClick(ClickEvent event) {
				sendIncomeSettingsToServer();
			}

			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) 
					sendIncomeSettingsToServer();
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendIncomeSettingsToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				
				sendButton.setEnabled(false);
				incomeSettingsService.updateIncome(incomeTextBox.getText(), outgoingTextBox.getText(),
						severableOutgoingTextBox.getText(),
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.setText("Remote Procedure Call - Failure");
								serverResponseLabel.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel.removeStyleName("serverResponseLabelError");
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
							}
						});
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);		
	}
	
	private void requestIncomeAndOutgoing()	{
		incomeSettingsService.retrieveIncome(
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						dialogBox.setPopupPosition(0, 0);
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						incomeTextBox.setText(result);
					}
				});
		incomeSettingsService.retrieveOutgoing(
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						dialogBox.setPopupPosition(0, 0);
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						outgoingTextBox.setText(result);
					}
				});		
		
		incomeSettingsService.retrieveSeverableOutgoing(
				new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						dialogBox.setPopupPosition(0, 0);
						closeButton.setFocus(true);
					}

					public void onSuccess(String result) {
						severableOutgoingTextBox.setText(result);
					}
				});		
				
	}
}
